﻿@inject NavigationManager Navigation
@inject IUserService userService
@inject IAuthService AuthService
@inject IConfigService configService

@if( userService.RolesEnabled )
{
    <AuthorizeView>
        <Authorized>
            <div class="damselfly-statusbarpanel" title="Logged in as @IdentityToolTip(context)">
                <i class="processing-icon fas fa-user"/>
                <NavLink href="/config">@context.User.Identity?.Name</NavLink>
            </div>
            <div>
                <button class="btn btn-primary" @onclick="LogOut" type="button">Log Out</button>
            </div>
        </Authorized>
        <NotAuthorized>
            <div>
                <button class="btn btn-primary" @onclick="Login" type="button">Log In</button>
            </div>
        </NotAuthorized>
    </AuthorizeView>
}

@code{

    private string IdentityToolTip(AuthenticationState state)
    {
        var adminRole = state.User.IsInRole(RoleDefinitions.s_AdminRole) ? " (Admin)" : string.Empty;
        return $"{state.User.Identity?.Name}{adminRole}";
    }

    async Task LogOut()
    {
        await AuthService.Logout();

        Navigation.NavigateTo(Navigation.Uri, true);
    }

    void Login()
    {
        Navigation.NavigateTo("Login");
    }

}